public class MoveZeroes {
    // leetcode 移动零
    // https://leetcode.cn/problems/move-zeroes/description/?envType=study-plan-v2&envId=top-100-liked

    /**
     * 通过slow指针和fast指针将数组分为三个部分
     * [0,slow]fast指针已遍历的元素中不为零的元素
     * (slow,fast)fast指针已遍历的元素中的零元素
     * [fast,length)待遍历的元素
     * @param nums
     */
    public void moveZeroes(int[] nums) {
        int slow = -1, fast = 0;
        while (fast < nums.length) {
            if (nums[fast] != 0) {
                swap(nums, ++slow, fast);
            }
            fast++;
        }
    }

    private void swap(int[] nums, int x, int y) {
        int tmp = nums[x];
        nums[x] = nums[y];
        nums[y] = tmp;
    }
}
